// com/tidu/disruptor/product/DataSyncEventProducer.java
package com.tidu.disruptor.product;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.tidu.disruptor.event.DataSyncEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DataSyncEventProducer {
    private final RingBuffer<DataSyncEvent> ringBuffer;

    public DataSyncEventProducer(RingBuffer<DataSyncEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    private static final EventTranslatorOneArg<DataSyncEvent, DataSyncEvent> TRANSLATOR =
            (event, sequence, dataEvent) -> {
                event.setData(dataEvent.getData());
                event.setBatchId(dataEvent.getBatchId());
                event.setOffset(dataEvent.getOffset());
                event.setBatchSize(dataEvent.getBatchSize());
            };

    public void onData(DataSyncEvent dataEvent) {
        ringBuffer.publishEvent(TRANSLATOR, dataEvent);
    }

    public long getRemainingCapacity() {
        return ringBuffer.remainingCapacity();
    }

    public long getBufferSize() {
        return ringBuffer.getBufferSize();
    }
}
